<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\repositories\bill;

use app\common\dao\bill\BillCourseDao;
use app\common\repositories\BaseRepository;
use think\facade\Db;

/**
 * Class BillCourseRepository
 *
 * @mixin BillCourseDao
 */
class BillCourseRepository extends BaseRepository
{

    public function __construct(BillCourseDao $dao)
    {
        $this->dao = $dao;
    }


    public function getPropertyList(array $where, $page, $limit, int $propertyId = null)
    {
        $query = $this->dao->search($where, $propertyId);
        $count = $query->count();
        $list = $query->page($page, $limit)
            ->with([
                'billInfo' => function ($query) {
                    $query->field('id,number,create_date,cutoff_date');
                    $query->bind(['number' => 'number', 'create_date' => 'create_date', 'cutoff_date' => 'cutoff_date']);
                },
                'roomInfo' => function ($query) {
                    $query->bind(['full_room_name']);
                },
                'payUserInfo' => function ($query) {
                    $query->field('id,username');
                    $query->bind(['pay_username' => 'username']);
                },
                'checkUserInfo' => function ($query) {
                    $query->field('id,username');
                    $query->bind(['check_username' => 'username']);
                },
                'affirmUserInfo' => function ($query) {
                    $query->field('id,username');
                    $query->bind(['affirm_username' => 'username']);
                },
            ])
            ->order('id', 'desc')
            ->select();
        return compact('list', 'count');
    }


    public function addInfo($propertyId, $data, int $userId)
    {
        return Db::transaction(function () use ($data, $propertyId, $userId) {
            $data['property_id'] = $propertyId;
            return $this->dao->create($data);
        });
    }

    public function editInfo($id, array $data, int $userId)
    {
        return Db::transaction(function () use ($data, $id, $userId) {

            return $this->dao->update($id, $data);
        });
    }


    public function batchDelete(array $ids, int $userId)
    {
        $list = $this->dao->selectWhere([
            ['id', 'in', $ids]
        ]);
        if ($list) {
            foreach ($list as $k => $v) {
                if ($v['bill_id'] > 0) {
                    $this->dao->delete($v['id']);
                }
            }
            return $list;
        }
        return [];
    }


    public function checkPayMomey($info, $param, int $userId)
    {
        return Db::transaction(function () use ($info, $param, $userId) {
            $param['check_time'] = date('Y-m-d H:i:s');
            $param['check_user_id'] = $userId ?? '0';
            $res = '';
            if ($param['check_type'] == 9) {
                ## 确认收款 与付款金额一致
                $param['affirm_type'] = 1;
                $res = $this->dao->update($info['id'], $param);
                ## 账单确认收款事件
                event('bill.confirmPay', [
                    'courseInfo' => $info,
                    'userId' => $userId,
                    'amount' => $param['check_amount']
                ]);
            } else if ($param['check_type'] == 2) {
                ## 确认收款 与付款金额不一致
                $param['check_amount'] = 0;
                $res = $this->dao->update($info['id'], $param);
            }
            return $res;
        });
    }


    public function affirmPayMomey($info, $param, int $userId)
    {
        return Db::transaction(function () use ($info, $param, $userId) {
            $param['affirm_time'] = date('Y-m-d H:i:s');
            $param['affirm_user_id'] = $userId ?? '0';
            $res = '';
            if ($param['affirm_type'] == 9) {
                ## 确认收款 与付款金额一致
                $res = $this->dao->update($info['id'], $param);
                ## 账单确认收款事件
//                event('property.bill.confirmPay', [
//                    'trackType' => $trackType,
//                    'billInfo' => $billInfo,
//                    'userId' => $userId,
//                    'amount' => $billInfo['pay_amount']
//                ]);
            } else if ($param['affirm_type'] == 2) {
                ## 确认收款 与付款金额不一致
                $res = $this->dao->update($info['id'], $param);

                ## 账单退回重新支付
//                event('property.bill.onBillReturnPay', [
//                    'trackType' => $trackType,
//                    'billInfo' => $billInfo,
//                    'remark' => $param['remark'],
//                    'userId' => $userId,
//                ]);
            }
            return $res;
        });
    }


    public function getDetail(int $id, int $propertyId = null)
    {
        $query = $this->dao->search([], $propertyId);
        $info = $query->where('id', $id)
            ->with([
                'billInfo' => function ($query) {
                    $query->field('id,number,create_date,cutoff_date');
                    $query->bind(['number' => 'number', 'create_date' => 'create_date', 'cutoff_date' => 'cutoff_date']);
                },
                'roomInfo' => function ($query) {
                    $query->bind(['full_room_name']);
                },
                'payUserInfo' => function ($query) {
                    $query->field('id,username');
                    $query->bind(['pay_username' => 'username']);
                },
                'checkUserInfo' => function ($query) {
                    $query->field('id,username');
                    $query->bind(['check_username' => 'username']);
                },
                'affirmUserInfo' => function ($query) {
                    $query->field('id,username');
                    $query->bind(['affirm_username' => 'username']);
                },
            ])
            ->find();

        return $info;
    }

}